package com.chujian.framework.starter.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.chujian.framework.auth.Accessor;
import com.chujian.framework.auth.AccessorHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * mybatisPlus自定义填充公共字段
 */
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增填充
     *
     * @param metaObject 对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        log.debug("=========开始自动填充字段=========");

        Accessor accessor = AccessorHolder.getAccessor();
        HashSet<String> setters = new HashSet<>(Arrays.asList(metaObject.getSetterNames()));

        if (accessor != null) {
            fillLongValue(metaObject, setters, "createUserId", accessor.getUserId());
            fillStringValue(metaObject, setters, "createUserName", accessor.getRealName());
            fillLongValue(metaObject, setters, "modifyUserId", accessor.getUserId());

        } else { // 缺省值
            fillLongValue(metaObject, setters, "createUserId", 0L);
            fillStringValue(metaObject, setters, "createUserName", "无");
            fillLongValue(metaObject, setters, "modifyUserId", 0L);
        }
        fillDateValue(metaObject, setters, "createTime", LocalDateTime.now());
        fillDateValue(metaObject, setters, "modifyTime", LocalDateTime.now());

        fillBooleanValue(metaObject, setters, "deleteFlag", Boolean.FALSE);
        log.debug("填充字段：" + metaObject.getValue("createUserId"));
    }


    /**
     * 更新填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Accessor accessor = AccessorHolder.getAccessor();
        HashSet<String> setters = new HashSet<>(Arrays.asList(metaObject.getSetterNames()));
        if (accessor != null) {
            fillLongValue(metaObject, setters, "modifyUserId", accessor.getUserId());
        } else {
            fillLongValue(metaObject, setters, "modifyUserId", 0L);
        }
        fillDateValue(metaObject, setters, "modifyTime", LocalDateTime.now());
    }

    private void fillIntValue(MetaObject metaObject, Set<String> setters, String param, Integer value) {
        if (setters.contains(param)) {
            this.strictInsertFill(metaObject, param, Integer.class, value);
        }
    }

    private void fillBooleanValue(MetaObject metaObject, Set<String> setters, String param, Boolean value) {
        if (setters.contains(param)) {
            this.strictInsertFill(metaObject, param, Boolean.class, value);
        }
    }

    private void fillLongValue(MetaObject metaObject, Set<String> setters, String param, Long value) {
        if (setters.contains(param)) {
            this.strictInsertFill(metaObject, param, Long.class, value);
        }
    }

    private void fillStringValue(MetaObject metaObject, Set<String> setters, String param, String value) {
        if (setters.contains(param)) {
            this.strictInsertFill(metaObject, param, String.class, value);
        }
    }

    private void fillDateValue(MetaObject metaObject, Set<String> setters, String param, LocalDateTime value) {
        if (setters.contains(param)) {
            this.strictInsertFill(metaObject, param, LocalDateTime.class, value);
        }
    }


}
